/**
 * @file lv_obj.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_obj_ex.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/



/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_obj_delete_all_child(lv_obj_t * obj)
{
    do{
        lv_obj_t *cur_child = lv_obj_get_child(obj, 0);
        if(cur_child == NULL) break;
        lv_obj_delete(cur_child);
    }while(1);
}

void lv_obj_delete_child(lv_obj_t * obj, lv_obj_t * child)
{
    do{
        lv_obj_t *cur_child = lv_obj_get_child(obj, 0);
        if(cur_child == NULL) break;
        if(cur_child == child){
            lv_obj_delete(child);
            break;
        }
    }while(1);
}

void lv_obj_delete_other_child(lv_obj_t * obj, lv_obj_t * child)
{
    uint32_t count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < count; ){
        lv_obj_t *cur_child = lv_obj_get_child(obj, i);
        if(cur_child == NULL) break;
        if(cur_child == child) i++;
        else lv_obj_delete(cur_child);
    }
}

void lv_obj_set_all_attrib0(lv_obj_t * obj, void(*fun)(lv_obj_t * obj))
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child);
    }
}

void lv_obj_set_all_attrib1(lv_obj_t * obj, lv_obj_fun1_t fun, uint32_t attrib1)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, attrib1);
    }
}

void lv_obj_set_all_attrib2(lv_obj_t * obj, lv_obj_fun2_t fun, uint32_t attrib1, uint32_t attrib2)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, attrib1, attrib2);
    }
}

void lv_obj_set_all_attrib3(lv_obj_t * obj,lv_obj_fun3_t fun, uint32_t attrib1, uint32_t attrib2, uint32_t attrib3)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, attrib1, attrib2, attrib3);
    }
}

void lv_obj_set_all_attrib0pv(lv_obj_t * obj, lv_obj_fun0pv_t fun, void *pv)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, pv);
    }
}
void lv_obj_set_all_attrib1pv(lv_obj_t * obj, lv_obj_fun1pv_t fun, void *pv, uint32_t attrib1)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, pv, attrib1);
    }
}

void lv_obj_set_all_attrib2pv(lv_obj_t * obj, lv_obj_fun2pv_t fun, void *pv, uint32_t attrib1, uint32_t attrib2)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, pv, attrib1, attrib2);
    }
}
void lv_obj_set_all_attrib3pv(lv_obj_t * obj, lv_obj_fun3pv_t fun, void *pv, uint32_t attrib1, uint32_t attrib2, uint32_t attrib3)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        fun(child, pv, attrib1, attrib2, attrib3);
    }
}


lv_event_dsc_t * lv_obj_add_all_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data)
{
    uint32_t child_count = lv_obj_get_child_count(obj);
    for(uint32_t i = 0; i < child_count; i++) {
        lv_obj_t * child = lv_obj_get_child(obj, i);
        lv_obj_add_event_cb(child, event_cb, filter, user_data);
    }
}
